package com.darksides.lendit.dao.jdo;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.darksides.lendit.dao.UserDAO;
import com.darksides.lendit.dao.jdo.entity.UserEntity;
import com.darksides.lendit.dao.utils.DAOUtils;
import com.darksides.lendit.exceptions.EmailAlreadyUsedException;
import com.darksides.lendit.exceptions.UserNotFoundException;
import com.darksides.lendit.resource.model.User;

@Repository
public class UserDAOJdo implements UserDAO {

	private static final Logger log = Logger.getLogger(UserDAOJdo.class.getName());
	private static final PersistenceManager pm = PMF.get().getPersistenceManager();
	
	@Autowired
	private Mapper mapper;

	@Autowired
	private DAOUtils daoUtils;
	
	public UserDAOJdo() {
		log.info("UserDAOJdo - constructor");
	}
	
	@Override
	public User createUser(User user) throws EmailAlreadyUsedException {
		
		if (!isFreeEmail(user.getEmail())) { 
			throw new EmailAlreadyUsedException(user.getEmail());
		}
		
		user.setId(null); //Create an user with Id null generated a new Id.
		UserEntity newUser = mapper.map(user, UserEntity.class);
		newUser.setIsActive(Boolean.TRUE);
		daoUtils.setInsertFields(newUser, "Z17BBARB");
		newUser = pm.makePersistent(newUser);
		return mapper.map(newUser, User.class);
		
	}
	
	@Override
	public User deleteUser(Long userId) throws UserNotFoundException {
		
		UserEntity userEntity;
		try {
			userEntity = pm.getObjectById(UserEntity.class, userId);
		} catch (JDOObjectNotFoundException e) {
			throw new UserNotFoundException(userId.toString());
		}
		if (Boolean.FALSE.equals(userEntity.getIsActive())) {
			//Account already disabled.
			throw new UserNotFoundException(userId.toString());
		}
		userEntity.setIsActive(Boolean.FALSE);
		daoUtils.setUpdateFields(userEntity, "Z17BBARB");
		pm.makePersistent(userEntity);
		
		return mapper.map(userEntity, User.class);
		
	}

	@Override
	public User updateUser(User user) {
		
		UserEntity updatedUser = mapper.map(user, UserEntity.class);
		daoUtils.setUpdateFields(updatedUser, "Z17BBARB");
		updatedUser = pm.makePersistent(updatedUser);
		return mapper.map(updatedUser, User.class);
		
	}

	@Override
	public User getUser(Long userId) {

		UserEntity userEntity = null;
		try {
			userEntity = pm.getObjectById(UserEntity.class, userId);
		} catch (JDOObjectNotFoundException e) {
			return null;
		}
		if (Boolean.FALSE.equals(userEntity.getIsActive())) return null;
		User user = mapper.map(userEntity, User.class);
		return user;
		
	}
	
	@SuppressWarnings("unchecked")
	private boolean isFreeEmail(String email) {
		Query query = pm.newQuery(UserEntity.class);
		query.setFilter("email == emailParam");
		query.declareParameters("String emailParam");
		List<UserEntity> results = (List<UserEntity>) query.execute(email);
		return results.isEmpty();
	}
	
}
